package com.ydx.aa76111.agent.runtime;

import org.jacoco.core.data.ExecutionDataWriter;
import org.jacoco.core.runtime.AbstractRuntime;
import org.jacoco.core.runtime.RuntimeData;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetAddress;

public class Agent {

    private static Agent singleton;
    private final RuntimeData data;

    public static synchronized Agent getInstance() {
        if (singleton == null) {
            singleton = new Agent();
        }
        return singleton;
    }

    private Agent() {
        this.data = new RuntimeData(this.createSessionId());
    }

    public RuntimeData getData() {
        return this.data;
    }

    private String createSessionId() {
        String host;
        try {
            host = InetAddress.getLocalHost().getHostName();
        } catch (Exception var3) {
            host = "unknownhost";
        }
        return host + "-" + AbstractRuntime.createRandomId();
    }

    public void reset() {
        this.data.reset();
    }

    public byte[] getExecutionData(boolean reset) {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        try {
            ExecutionDataWriter writer = new ExecutionDataWriter(buffer);
            this.data.collect(writer, writer, reset);
        } catch (IOException var4) {
            throw new AssertionError(var4);
        }
        return buffer.toByteArray();
    }
}
